package com.gxc.authcenter.server.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.gxc.authcore.system.domain.User;
import com.gxc.authcore.system.util.CurrentUtil;
import com.gxc.authcore.system.util.ParamUtil;
import com.gxc.authcore.system.util.SerializeUtil;
import com.gxc.webcore.util.RequestUtil;

@Controller("common/authserverController")
public class AuthServerController {

	/**
	 * 服务端（认证中心）调用
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public ModelAndView getAuthId(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map params = RequestUtil.getInMap(request);
		// 获取认证后的跳回上次的请求地址
		String clientSystemUrl = request.getParameter("clientSystemUrl");
		// authid
		String authid = CurrentUtil.getAuthid();
		User user = CurrentUtil.getAuthUser();
		if (user != null) {
			params.put("authid", authid);
			response.sendRedirect(clientSystemUrl
					+ "/common/authclient/setAuthId.do"
					+ SerializeUtil.serializeParam(params));
		} else {
			// 重定向至登陆页面
			String loginUrl = ParamUtil.get("url_authcenter")
					+ "/common/auth/login.do";
			response.sendRedirect(loginUrl
					+ SerializeUtil.serializeParam(params));
		}
		return null;
	}

}
